<template>
    <div>
       <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >

          <el-form-item label="用户名称">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              size="small"
              style="width: 260px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="用户部门" >
            <treeselect :default-expand-level="1" v-model="queryParams.orgId" :options="deptOptions"
            :show-count="true" placeholder="请选择" style="width: 300px;"
            />
          </el-form-item>
          <el-form-item label="用户角色">
            <el-select v-model="queryParams.roleId"  placeholder="请选择" @change="getList" style="width: 260px;">
              <el-option
                v-for="item in roleOptions"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
                :disabled="item.status == 1"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="20">
         <!--用户数据-->
        <el-col :span="16" :xs="24">
        <el-table v-loading="loading" :data="userList" @row-click="chooseUser" height="400px" style="overflow-y: auto;">
          <el-table-column label="工号" align="center" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column label="姓名" align="center" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="部门" align="center" prop="orgName" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" prop="phonenumber" width="120" />
          <el-table-column label="状态" align="center" prop="status" :formatter="userStatusFormat"/>
        </el-table>
        </el-col>
        <!--已经选择的用户数据-->
        <el-col :span="8" :xs="24">
          <el-table :data="choosedUserList" @row-click="unchooseUser"  height="400px" style="overflow-y: auto;">
            <el-table-column label="已选工号" align="center" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="姓名" align="center" prop="nickName" :show-overflow-tooltip="true" />
          </el-table>
        </el-col>
    </el-row>
    <el-row>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
     <el-button icon="el-icon-check"type="primary" @click="submitChoose" size="small">确定</el-button>
     <el-button icon="el-icon-close" @click="cancel" size="small">取 消</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryUser } from "@/api/labdata/employee";
import { allRole } from "@/api/system/role";
import { treeselect } from "@/api/labdata/orgnization";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "UserChoose",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      callType:null,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      choosedUserList:[],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 状态数据字典
      statusOptions: [],
      // 岗位选项
     //postOptions: [],
      // 角色选项
      roleOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        orgId: undefined,
        roleId: undefined,
        postId: undefined
      },
      // 表单校验
      rules: {

      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    'queryParams.orgId': 'currDeptChange',
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    allRole().then(response => {
      //this.postOptions = response.posts;
      this.roleOptions = response.roles;
    });
  },
  methods: {
    submitChoose(){
      //父类注册组件，调用方式
      //<UserChoose @handleChooseUser="getUser" ></UserChoose>
      //调用注册的父类方法
      //把已经选择的用户信息 choosedUserList 传递给父类 handleChooseUser 方法
      this.$emit("chooseUsers", this.choosedUserList, this.callType);
      this.open= false;
    },
    currDeptChange(val) {
       //console.log('currDeptChange', val);
       if(val){
         //console.log('call query');
         this.getList();
      }
    },
    chooseUser(row){
      //console.log(row);
      for(var i=0;i<this.userList.length;i++){
        if(this.userList[i].userId==row.userId ||
           this.userList[i].userName==row.userName){
            var choosed = false;
            for(var j=0;j<this.choosedUserList.length;j++){
              if(this.choosedUserList[j].userId == row.userId ||
                 this.choosedUserList[j].userName == row.userName){
                choosed = true;
              }
            }
            if(!choosed){
              this.choosedUserList.push(this.userList[i]);
            }
        }
      }
      for(var j=0;j<this.choosedUserList.length;j++){
         for(var i=0;i<this.userList.length;i++){
           if(this.userList[i].userId == this.choosedUserList[j].userId ||
              this.userList[i].userName == this.choosedUserList[j].userName
              ){
             //console.log('userlist:'+this.userList[i].userId)
             //console.log('chooseedlist:'+this.choosedUserList[j].userId)
             this.userList.splice(i,1);
             i--;
           }
         }
      }
  },
    unchooseUser(row){
      for(var j=0;j<this.choosedUserList.length;j++){
        if(this.choosedUserList[j].userId == row.userId ||
          this.choosedUserList[j].userName == row.userName){
          this.userList.push(this.choosedUserList[j]);
          this.choosedUserList.splice(j,1);

        }
      }
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      queryUser(this.queryParams).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 任职状态字典翻译
    userStatusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
 /** 新增按钮操作 */
    handleAdd(checkedUsers, callType) {
      if(checkedUsers){
        this.setChoosedUser(checkedUsers);
      }else{
        this.choosedUserList = [];
      }
      if(callType){
         this.callType = callType;
      }
      this.open = true;
      this.getList();
      this.title = "选择工作人员";
    },
    setChoosedUser(checkedList){
      this.choosedUserList = [];
      for(var j=0;j<checkedList.length;j++){
         for(var i=0;i<this.userList.length;i++){
           if(this.userList[i].userId == checkedList[j].userId ||
            this.userList[i].userName == checkedList[j].userName){
            this.choosedUserList.push(this.userList[i]);
             //console.log('userlist:'+this.userList[i].userId)
             //console.log('chooseedlist:'+this.choosedUserList[j].userId)
             this.userList.splice(i,1);
             i--;
           }
         }
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
      pageNum: 1,
      pageSize: 10,
      userName: undefined,
      orgId: undefined,
      roleId: undefined,
      postId: undefined
      }
      this.handleQuery();
    },

  }
};
</script>
<style lang="scss" scoped>
//表格鼠标悬停的样式（背景颜色）
/deep/ {
  .el-table {
    tbody tr {
      &:hover {
        td {
          cursor: pointer;
        }
      }
    }
  }
}
/deep/ .el-form-item {
    margin-bottom: 10px;
}
/deep/ .el-dialog__body {
    padding: 0px 20px;
}
</style>
